package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.entity.family.ClassificationInfo;
import com.tuanzi.loan.business.entity.family.CommSysCode;
import com.tuanzi.loan.business.entity.family.MemberRights;
import com.tuanzi.loan.business.service.family.ClassificationService;
import com.tuanzi.loan.business.service.family.LoanAdvertService;
import com.tuanzi.loan.business.service.family.MemberRightsService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.MemberRightsView;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class MemberRightsInfoBean extends JSFInfoBean<MemberRightsView> {

    @Autowired
    private MemberRightsService memberRightsService;

    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private LoanAdvertService loanAdvertService;

    public List<ClassificationInfo> classificationInfos;

    private List<CommSysCode> commSysCodes;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isEmpty(id)) {
            $ = new MemberRightsView();
            $.setNeed_login(false);
            $.setSpecial_show(false);
            changeStatusToAdd();
        }else{
            long mid = Long.parseLong(id);
            $ = memberRightsService.getMemberRightsView(mid);
        }
        classificationInfos = classificationService.loadClassifications();
        commSysCodes = loanAdvertService.findAdvert();
    }

    public String onSave(){
        if (validate() == false) return null;
        try {
            MemberRights memberRights = memberRightsService.saveOrUpdateRights($);
            changeStatusToView();
            $.setId(memberRights.getId());
            addSuccessfullyMessage();
        } catch (Exception e) {
            addErrorMessage("系统异常");
            e.printStackTrace();
        }
        return null;
    }

    public void onUploadRightsIcon(FileUploadEvent event) {
        $.setIcon_url(this.uploadImg(event.getFile()));
    }


    private boolean validate() {
        if(StringUtils.isEmpty($.getRights_name())){
            addErrorMessage("权益名称不能为空");
            return false;
        }
        if(StringUtils.isEmpty($.getRights_title())){
            addErrorMessage("权益标题不能为空");
            return false;
        }
        if(null == $.getType()){
            addErrorMessage("权益类型不能为空");
            return false;
        }
        if($.getPlatform_type() == null || $.getPlatform_type().size() <=0){
            addErrorMessage("平台类型不能为空");
            return false;
        }
        if(null == $.getClassification_id()){
            addErrorMessage("所属组不能为空");
            return false;
        }
        if(null == $.getSort()){
            addErrorMessage("排序不能为空");
            return false;
        }
        if(null == $.getStart_time()){
            addErrorMessage("开始时间不能为空");
            return false;
        }
        if(null == $.getEnd_time()){
            addErrorMessage("结束时间不能为空");
            return false;
        }
        if(null == $.getStatus()){
            addErrorMessage("上线状态不能为空");
            return false;
        }
        if(null == $.getUser_type() || $.getUser_type().size() <= 0){
            addErrorMessage("用户类型不能为空");
            return false;
        }
        if(StringUtils.isEmpty($.getIcon_url())){
            addErrorMessage("图标不能为空");
            return false;
        }
//        if(StringUtils.isEmpty($.getUrl())){
//            addErrorMessage("跳转链接不能为空");
//            return false;
//        }
        if(null == $.getProduct_id() || $.getProduct_id().size() <= 0){
            addErrorMessage("应用不能为空");
            return false;
        }
        return true;
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(ONLINE_OFFLINE_STATUS);
    }

    @Override
    public List<CommProductId> getCommProductIds() {
        return super.getCommProductIds().stream().filter(commProductId -> commProductId.getRealPrdid() != null).collect(Collectors.toList());
    }

    public void onSelectAllProduct() {
        if (CollectionUtils.isEmpty($.getProduct_id()) || $.getProduct_id().size() < getCommProductIds().size()) {
            $.setProduct_id(getCommProductIds().stream().map(product -> product.getProductId().toString()).collect(Collectors.toList()));
        } else {
            $.getProduct_id().clear();
        }
    }
}
